package com.ht.api.websocket;

import java.io.IOException;

import com.ht.api.bean.AjaxResult;
import com.ht.api.handler.auth.ICheck;

import jakarta.annotation.Resource;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * 校验登陆
 * @author lenovo
 * @createTime 2024年8月21日 下午2:35:09
 */
@Slf4j
public class WebSocketFilter implements Filter {
	@Resource
	private ICheck checkLogin;

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		// 校验是否登陆
		AjaxResult rb = checkLogin.check(request, response);
		if (rb.isSuccess()) {
			filterChain.doFilter(servletRequest, servletResponse);
		} else {
			log.warn("[websocket]未登陆不允许建立连接~");
			return;
		}
	}

}
